#ifndef RESIZABLERECTITEM_H
#define RESIZABLERECTITEM_H

#include <QGraphicsRectItem>
#include <QGraphicsSceneMouseEvent>
#include <QRectF>
#include <QPointF>
#include <QPainter>

enum ResizeHandle {
    NoHandle,
    TopLeft,
    TopRight,
    BottomLeft,
    BottomRight,
    Left,
    Right,
    Top,
    Bottom
};

class ResizableRectItem : public QGraphicsRectItem
{
public:
    ResizableRectItem(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = nullptr);

    void setResizable(bool resizable);          // 设置可调整
    bool isResizable() const;                   // 是否可调整
    void setSolidBorderColor(QColor color);     // 设置实现边框颜色

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
    void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;
    QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
    QRectF boundingRect() const override;

private:
    ResizeHandle getHandleAtPosition(const QPointF &pos);
    QRectF getHandleRect(ResizeHandle handle);
    void updateCursor(ResizeHandle handle);

    bool m_resizable;               // 可调整标志位，现在多余
    bool m_resizing;                // 正在调整标志位，当点击矩形边框八个点的时候设置为true
    ResizeHandle m_currentHandle;   // 方位
    QPointF m_originalPos;
    QRectF m_originalRect;

    // 自定义样式成员
    qreal m_handleSize = 5;                // 控制点大小
    QColor m_solidBorderColor = Qt::blue; // 实线边框颜色
    QColor m_dashBorderColor = Qt::blue;   // 高亮虚线颜色（选中时）
    QColor m_handleColor = Qt::white;     // 控制点颜色
};

#endif // RESIZABLERECTITEM_H
